package com.pig.util;

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class CutImage extends JFrame {

	private Image back;

	private Image wolf;

	private Image[] wolves;

	private Image a;

	private Image background;

	private Image ball;

	private Image[] balls;

	private Image bullet;

	private Image cord;

	private Image dead;

	private Image fruit;

	private Image gear;

	private Image[] gears;

	private Image help;

	private Image login;

	private Image menu;

	private Image[] menus;

	private Image options;

	private Image paihang;

	private Image pig1;

	private Image pig2;

	private Image pig3;

	private Image pig4;

	private Image pig5;

	private Image pig;

	private Image[] pigs;

	private Image umbrella;

	private MediaTracker mt;

	public CutImage() {
		loadImage();
		loadImageToMediaTracker();
	}

	private void loadImage() {
		back = this.getImage("images/back.JPG");
		wolf = this.getImage("images/wolf.gif");
		a = this.getImage("images/1.gif");
		background = this.getImage("images/background.jpg");
		ball = this.getImage("images/ball.gif");
		bullet = this.getImage("images/bullet.gif");
		cord = this.getImage("images/cord.gif");
		dead = this.getImage("images/dead.gif");
		fruit = this.getImage("images/fruit.gif");
		gear = this.getImage("images/gear.gif");
		help = this.getImage("images/help.JPG");
		login = this.getImage("images/login.jpg");
		menu = this.getImage("images/menu.gif");
		options = this.getImage("images/options.jpg");
		paihang = this.getImage("images/paihang.JPG");
		pig1 = this.getImage("images/pig1.gif");
		pig2 = this.getImage("images/pig2.gif");
		pig3 = this.getImage("images/pig3.gif");
		pig4 = this.getImage("images/pig4.gif");
		pig5 = this.getImage("images/pig5.gif");
		pig = this.getImage("images/pig.gif");
		umbrella = this.getImage("images/umbrella.gif");
		wolves = this.loadImageStrip(wolf, 14, 125, 125, 0);
		balls = this.loadImageStrip(ball, 4, 100, 80, 0);
		gears = this.loadImageStrip(gear, 2, 103, 59, 0);
		menus = this.loadImageStrip(menu, 8, 200, 100, 0);
		pigs = this.loadImageStrip(pig, 10, 100, 83, 0);
	}

	private Image[] loadImageStrip(Image image, int numImages, int cellWidth,
			int cellHeight, int cellBorder) {
		Image[] images = new Image[numImages];
		MediaTracker tMT = new MediaTracker(this);
		tMT.addImage(image, 0);
		try {
			tMT.waitForAll();
		} catch (InterruptedException e) {
			JOptionPane.showMessageDialog(null, "Image Loading Failed!");
			e.printStackTrace();
		}
		ImageProducer ip = image.getSource();
		int imgCols = image.getWidth(this) / cellWidth;
		for (int i = 0; i < numImages; i++) {
			int x = (i % imgCols) * cellWidth + cellBorder;
			int y = (i / imgCols) * cellHeight + cellBorder;
			images[i] = this.loadCell(ip, x, y, cellWidth, cellHeight);
		}
		return images;
	}

	private Image loadCell(ImageProducer ip, int x, int y, int width, int height) {
		Image cellImage = this.createImage(new FilteredImageSource(ip,
				new CropImageFilter(x, y, width, height)));
		return cellImage;
	}

	public Image getImage(String imagePath) {
		FileUtil fUtil = new FileUtil();
		String realPath = fUtil.getFilePath(imagePath);
		Image image = Toolkit.getDefaultToolkit().getImage(realPath);
		return image;
	}

	public void loadImageToMediaTracker() {
		mt = new MediaTracker(this);
		mt.addImage(wolf, 0);
		mt.addImage(back, 0);
		mt.addImage(a, 0);
		mt.addImage(background, 0);
		mt.addImage(ball, 0);
		mt.addImage(bullet, 0);
		mt.addImage(cord, 0);
		mt.addImage(dead, 0);
		mt.addImage(fruit, 0);
		mt.addImage(gear, 0);
		mt.addImage(help, 0);
		mt.addImage(login, 0);
		mt.addImage(menu, 0);
		mt.addImage(options, 0);
		mt.addImage(paihang, 0);
		mt.addImage(pig1, 0);
		mt.addImage(pig2, 0);
		mt.addImage(pig3, 0);
		mt.addImage(pig4, 0);
		mt.addImage(pig5, 0);
		mt.addImage(pig, 0);
		mt.addImage(umbrella, 0);
		for (int i = 0; i < wolves.length; i++) {
			mt.addImage(wolves[i], 0);
		}

		for (int i = 0; i < balls.length; i++) {
			mt.addImage(balls[i], 0);
		}

		for (int i = 0; i < gears.length; i++) {
			mt.addImage(gears[i], 0);
		}

		for (int i = 0; i < menus.length; i++) {
			mt.addImage(menus[i], 0);
		}

		for (int i = 0; i < pigs.length; i++) {
			mt.addImage(pigs[i], 0);
		}

		try {
			mt.waitForAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public Image getA() {
		return a;
	}

	public Image getBack() {
		return back;
	}

	public Image getBackgroundImage() {
		return background;
	}

	public Image getBall() {
		return ball;
	}

	public Image[] getBalls() {
		return balls;
	}

	public Image getBullet() {
		return bullet;
	}

	public Image getCord() {
		return cord;
	}

	public Image getDead() {
		return dead;
	}

	public Image getFruit() {
		return fruit;
	}

	public Image getGear() {
		return gear;
	}

	public Image[] getGears() {
		return gears;
	}

	public Image getHelp() {
		return help;
	}

	public Image getLogin() {
		return login;
	}

	public Image getMenu() {
		return menu;
	}

	public Image[] getMenus() {
		return menus;
	}

	public MediaTracker getMt() {
		return mt;
	}

	public Image getOptions() {
		return options;
	}

	public Image getPaihang() {
		return paihang;
	}

	public Image getPig() {
		return pig;
	}

	public Image getPig1() {
		return pig1;
	}

	public Image getPig2() {
		return pig2;
	}

	public Image getPig3() {
		return pig3;
	}

	public Image getPig4() {
		return pig4;
	}

	public Image getPig5() {
		return pig5;
	}

	public Image[] getPigs() {
		return pigs;
	}

	public Image getUmbrella() {
		return umbrella;
	}

	public Image getWolf() {
		return wolf;
	}

	public Image[] getWolves() {
		return wolves;
	}

}
